package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 处理请求和响应的方法
 *
 * @version V1.0
 * @package com.leyou.item.web
 * @description:
 * @author: pc
 * @date: 2019/6/27 16:08
 */
@RestController
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    //根据id查询spu，最好同时查询出spuDetail和skus，这样比分三次查询效率较高。（没有这样的接口）
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long spuId) {
        return ResponseEntity.ok(goodsService.querySpuById(spuId));
    }

    //分页查询数据
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows
    ) {
        PageResult<SpuDTO> result = goodsService.querySpuByPage(key, saleable, page, rows);

        return ResponseEntity.ok(result);
    }

    //保存商品信息
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {

        this.goodsService.saveGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //修改商品信息
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        this.goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    //商品上下架
    @PutMapping("spu/saleable")
    public ResponseEntity<Void> putGoods(@RequestParam("id") Long id, @RequestParam("saleable") Boolean saleable) {

        this.goodsService.putGoods(id, saleable);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //查询商品附加信息
    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id) {
        SpuDetailDTO spuDetailDTO = goodsService.querySpuDetailById(id);
        return ResponseEntity.ok(spuDetailDTO);
    }

    //查询商品特殊信息
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuById(@RequestParam("id") Long id) {
        return ResponseEntity.ok(goodsService.querySkuById(id));
    }

    /**
     * 根据id批量查询sku
     *
     * @param ids skuId的集合
     * @return sku的集合
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(this.goodsService.querySkuListByIds(ids));
    }

    /**
     * 减库存
     *
     * @param cartMap 商品id及数量的map
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long,Integer> cartMap) {
        goodsService.minusStock(cartMap);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
